import maya.cmds as mc 

import LZ_public_command.LZ_rivet as LZ_rivet
reload(LZ_rivet)

class reset_mouthCtrl_positon(LZ_rivet.LZ_rivetClass):
    def __init__(self):
        pass
    def disconnect_ctrlsetting(self ,prefix='', mesh_rivet =  'head_geo_rivet_EX' ,ctrl = [u'ac_lf_cornerlip_ctrl',u'ac_lf_uplip2_ctrl',u'ac_lf_uplip1_ctrl',u'ac_lf_lowlip2_ctrl',u'ac_lf_lowlip1_ctrl',u'ac_cn_lowlip_ctrl',u'ac_cn_uplip_ctrl',u'ac_rt_uplip1_ctrl',u'ac_rt_lowlip1_ctrl',u'ac_rt_lowlip2_ctrl',u'ac_rt_uplip2_ctrl',u'ac_rt_cornerlip_ctrl'] ):
        curve_mouth = prefix + 'mouth_lip_deformer_curve01'
        
        modifyCurve = str(curve_mouth)+'_modify'
        correctCurve = str(curve_mouth)+'_correct'
        if mc.objExists(correctCurve) == False:
            mc.duplicate( curve_mouth , n = correctCurve )
            blendshapenode = str(correctCurve)+'_blendnode'
            mc.blendShape(correctCurve , curve_mouth ,frontOfChain=True , n = blendshapenode)
            mc.setAttr(str(blendshapenode)+'.' + str(correctCurve) , 1 )
        
        secJntGrp = prefix + 'ac_cn_mouth_secjnt_grp'
        allChilddren = mc.listRelatives( secJntGrp , c=True , p=False)
        
        if mc.attributeQuery( 'correct' , node =curve_mouth , ex =True ) == False:
            mc.addAttr( curve_mouth  ,ln = 'correct' ,  dv = 1 )
            mc.setAttr( str(curve_mouth) + '.correct' , k =True )
            
            
            for ct in ctrl :
                
                rivetGrp = str(str(ct).replace('_ctrl' , '_rivet'))
                follicle = str(rivetGrp)+'_follicle'
                mc.delete(follicle)
                
 
            
            if mc.objExists(modifyCurve) == False:
                mc.duplicate( curve_mouth , n = modifyCurve )
                blendshapenode = str(modifyCurve)+'_blendnode'
                mc.blendShape(modifyCurve , correctCurve ,frontOfChain=True ,  n = blendshapenode)
                mc.setAttr(str(blendshapenode)+'.' + str(modifyCurve) , 1 )
            
            mc.parent(modifyCurve , world =True)
            
            mc.setAttr(str(modifyCurve) + '.v' , lock =False )
            mc.setAttr(str(modifyCurve) + '.v' , 1 )
            


            for ac in allChilddren:
                conGrp  = mc.listRelatives( ac , c=True ,p=False )
                locator_con = str(conGrp[0])+'_rivetloc'
                
                mc.delete(locator_con)
            
            
            mc.setAttr( str(curve_mouth) + '.correct' , 0)
        else:
            if_correct = mc.getAttr(str(curve_mouth) + '.correct')
            
            if if_correct == 1:
                
                
                for ct in ctrl :
                    
                    rivetGrp = str(str(ct).replace('_ctrl' , '_rivet'))
                    follicle = str(rivetGrp)+'_follicle'
                    mc.delete(follicle)
                    
                if mc.objExists(modifyCurve) == False:
                    mc.duplicate( curve_mouth , n = modifyCurve )
                    blendshapenode = str(modifyCurve)+'_blendnode'
                    mc.blendShape(modifyCurve , curve_mouth , n = blendshapenode)
                    mc.setAttr(str(blendshapenode)+'.' + str(modifyCurve) , 1 )
                
                mc.parent(modifyCurve , world =True)
                
                mc.setAttr(str(modifyCurve) + '.v' , lock =False )
                mc.setAttr(str(modifyCurve) + '.v' , 1 )
                


                for ac in allChilddren:
                    conGrp  = mc.listRelatives( ac , c=True ,p=False )
                    locator_con = str(conGrp[0])+'_rivetloc'
                    
                    mc.delete(locator_con)
                
                
                mc.setAttr( str(curve_mouth) + '.correct' , 0)

            else:
                for ct in ctrl :
                    
                    rivetGrp = str(str(ct).replace('_ctrl' , '_rivet'))
                    self.LZ_rivet( motherType = 'mesh',inputObj =  mesh_rivet, rivetObj = [rivetGrp] , IsRotate = 0 )
                

                
                for ac in allChilddren:
                    conGrp  = mc.listRelatives( ac , c=True ,p=False )  
                    self.LZ_rivet( motherType = 'curve',inputObj = curve_mouth , rivetObj = [conGrp[0]] , IsRotate = 0 )
                    
                mc.setAttr( str(curve_mouth) + '.correct' , 1 )
                    
                mc.delete(modifyCurve)
                    